/**
*  
* @Function 共享定制服务 
* @ProjectName credit-data-platform 
* @PackageName com.py.credit.service.impl    
* @FileName ShareCustomizeServiceImpl.java    
* @Date 2018年9月11日下午5:42:01
* @Author Min DongXv
* @Copyright (c) 2018, 金电联行 All Rights Reserved.   
*   
*/
package com.py.credit.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.py.credit.dao.ShareCustomizeDao;
import com.py.credit.dao.ShareResourceDao;
import com.py.credit.entity.DeptShareResourceEntity;
import com.py.credit.entity.SContentInfo;
import com.py.credit.entity.ShareCustomizeEntity;
import com.py.credit.entity.common.TreeObj;
import com.py.credit.service.ScontentService;
import com.py.credit.service.ShareCustomizeService;
import com.py.credit.utils.SQLBuilder;
import com.py.framework.core.common.Page;

/**
 * 
 * @Function 共享定制服务
 * @ClassName ShareCustomizeServiceImpl
 * @Date 2018年9月11日 下午5:42:01
 * @Author Min DongXv
 * 
 */
@Service
public class ShareCustomizeServiceImpl implements ShareCustomizeService {

	private static final String CHECKED = "true";

	private static final String NO_CHECKED = "false";

	@Autowired
	private ShareCustomizeDao shareCustomizeDao;

	@Autowired
	private ShareResourceDao shareResourceDao;

	@Autowired
	private ScontentService scontentService;

	/**
	 *
	 * @Function 分页查询共享定制信息
	 * @FileName ShareCustomizeServiceImpl.java
	 * @MethodName shareCustomizeListByConditionForPage
	 * @param columns
	 * @param additionalColumns
	 * @param condition
	 * @param page
	 * @return
	 * @Date 2018年9月11日 下午5:44:02
	 * @Author Min DongXv
	 *
	 */
	@Override
	public List<Map<String, Object>> shareCustomizeListByConditionForPage(List<String> columns,
			String[] additionalColumns, String condition, Page page) {
		List<String> conditions = SQLBuilder.createConditionSql(condition);
		return shareCustomizeDao.shareCustomizeListByConditionForPage(columns, additionalColumns, conditions, page);
	}

	/**
	 *
	 * @Function 新增共享定制
	 * @FileName ShareCustomizeServiceImpl.java
	 * @MethodName addShareCustomize
	 * @param deptId
	 *            部门ID
	 * @param shareDate
	 *            共享期限
	 * @param managerId
	 *            操作人ID
	 * @param customizeStatus
	 * @Date 2018年9月11日 下午5:44:02
	 * @Author Min DongXv
	 *
	 */
	@Override
	public void addShareCustomize(Long deptId, String shareDate, Long managerId, String customizeStatus) {
		ShareCustomizeEntity entity = shareCustomizeDao.getShareCustomizeByDeptId(deptId);
		if (null == entity) {
			shareCustomizeDao.addShareCustomize(deptId, shareDate, managerId, customizeStatus);
		}
	}

	/**
	 *
	 * @Function 更新共享定制
	 * @FileName ShareCustomizeServiceImpl.java
	 * @MethodName updateShareCustomize
	 * @param shareDate
	 *            共享期限
	 * @param managerId
	 *            操作人ID
	 * @param customizeStatus
	 *            定制状态
	 * @Date 2018年9月11日 下午5:44:02
	 * @Author Min DongXv
	 *
	 */
	@Override
	@Transactional
	public void updateShareCustomize(Long id, String shareDate, Long managerId, String customizeStatus,
			List<DeptShareResourceEntity> resources, Long deptId) {
		shareCustomizeDao.updateShareCustomize(id, shareDate, managerId, customizeStatus);
		shareResourceDao.delShareResource(deptId);
		shareResourceDao.batchAddShareResource(resources);
	}

	/**
	 *
	 * @Function 共享定制是否存在
	 * @FileName ShareCustomizeServiceImpl.java
	 * @MethodName getShareCustomizeByDeptId
	 * @param deptId
	 *            部门ID
	 * @return
	 * @Date 2018年9月11日 下午5:44:02
	 * @Author Min DongXv
	 *
	 */
	@Override
	public ShareCustomizeEntity getShareCustomizeByDeptId(Long deptId) {
		return shareCustomizeDao.getShareCustomizeByDeptId(deptId);
	}

	/**
	 *
	 * @Function 获取定制详情
	 * @FileName ShareCustomizeServiceImpl.java
	 * @MethodName shareCustomizeDetail
	 * @param deptId
	 * @return
	 * @Date 2018年9月11日 下午8:13:05
	 * @Author Min DongXv
	 *
	 */
	@Override
	public ShareCustomizeEntity shareCustomizeDetail(Long deptId) {
		ShareCustomizeEntity entity = getShareCustomizeByDeptId(deptId);
		if (null != entity && 0 <= entity.getId()) {
			SContentInfo sContentInfo = scontentService.getShareContent("YZS");
			if (null != sContentInfo) {
				entity.setContentName(sContentInfo.getName());
				entity.setContenType("一站式");
				List<String> resources = shareResourceDao.getShareResource(deptId);
				List<TreeObj> trees = scontentService.getEditDeatil(sContentInfo.getId());
				if (!CollectionUtils.isEmpty(trees)) {
					for (TreeObj t : trees) {
						if (resources.contains(t.getId())) {
							t.setChecked(CHECKED);
						} else {
							t.setChecked(NO_CHECKED);
						}
					}
				}
				entity.setResourceList(trees);
			}
		}
		return entity;
	}
}
